define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    require('ztree');
    // require('ztreeExhide');
    $('#form-user').validate({
        success: function () {
            updateRole();
            return false;
        }
    });
    // $(document).on('click','.create-user',function () {
    //     // createUser();
    // });

    function updateRole() {

        var roleName = $.trim($('#roleName').val());
        var roleRemark = $.trim($('#roleRemark').val());
        var roleId = $('.back_role').attr('data-id');
        var zTree = $.fn.zTree.getZTreeObj("ztree");
        var nodes = zTree.getChangeCheckedNodes();
        var privIds = new Array();
        $.each(nodes, function (i, item) {
            privIds[i] = item.id;
        });
        var data = {
            roleId: roleId,
            roleName: roleName,
            roleRemark: roleRemark,
            privIds: JSON.stringify(privIds)
        };

        $('.edit-role').prop('disabled', 'disabled');
        $.post('/role/update', data, function (o) {
            switch (o.code) {
                case 0:
                    toastSuccess('编辑成功');
                    setTimeout(function () {
                        window.location.href = '/role'
                    }, 1000);
                    break;
                default:
                    toastError(o.msg);
                    $('.edit-role').removeAttr('disabled');
            }
        })


    }

    function initZtree() {
        var roleId = $('.back_role').attr('data-id');
        $.post('/role/privList', {roleId: roleId}, function (o) {
            switch (o.code) {
                case 0:
                    var list = o.data.list;
                    var hasList = o.data.hasList;
                    initZtreeData(list, hasList);
                    break;
                default:
                    toastError(o.msg);
            }
        });
    }

    function initZtreeData(list, hasList) {
        var zNodes = new Array();
        $.each(list, function (i, item) {
            zNodes[i] = {id: item.privId, pId: item.pprivId, name: item.privName, open: true};
        });
        var setting = {
            check: {
                enable: true,
                nocheckInherit: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        $.fn.zTree.init($("#ztree"), setting, zNodes);

        var treeObj = $.fn.zTree.getZTreeObj("ztree");
        $.each(hasList, function (i, item) {
            var node = treeObj.getNodeByParam("id", item.privId, null);
            if (node != null) {
                treeObj.checkNode(node, true);
            }
        });
    }

    initZtree();

});
